using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Xml;

public class GameRessource {
	
	/// <summary>
    /// Fichier XML qui les stats des persos et les stats des spells
    /// </summary>
    public TextAsset xmlPersonnage;
	public TextAsset xmlSpells;
	
	public Dictionary<string, int []> classDictionary;
	public Dictionary<int, Spells> spellsDictionary;
	public Texture2D[] TableauTextureClass;
	

	public GameRessource(TextAsset xmlPersonnage, TextAsset xmlSpells)
    {
		this.xmlPersonnage = xmlPersonnage;
        this.xmlSpells = xmlSpells;
        LoadSpells();
		LoadClass();
		TableauTextureClass = new Texture2D[3];
		TableauTextureClass[0]= ImagePerso(1);
		TableauTextureClass[1]= ImagePerso(2);
		TableauTextureClass[2]= ImagePerso(3);
    }
	
	
	/// <summary>
    /// Charge les données (classes) du XML dans les dictionnaires
    /// </summary>
    /// 
	public void LoadClass(){
		XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.LoadXml(xmlPersonnage.text);
		classDictionary = new Dictionary<string, int []>();
		//Chargement du dico de perso
        foreach (XmlNode persoNode in xmlDoc.SelectNodes("persodata/perso"))
        {
			XmlNodeList idlist=persoNode.SelectNodes("spell/id");
			int[] idspells= new int [idlist.Count];
            string name = persoNode["name"].InnerText;
			for (int i = 0;i<idlist.Count;i++)
        	{
				 idspells[i] = int.Parse(idlist[i].InnerText);
			}
            classDictionary.Add(name, idspells);
        }
	}
	
	/// <summary>
    /// Charge les données (spells) du XML dans les dictionnaires
    /// </summary>
    /// 
	public void LoadSpells(){
		XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.LoadXml(xmlSpells.text);
		spellsDictionary = new Dictionary<int, Spells>();
		//Chargement du dico de spell
		foreach (XmlNode persoNode in xmlDoc.SelectNodes("spelldata/spell"))
        {
			int idSpell = int.Parse(persoNode["id"].InnerText);
			string nameSpell = persoNode["name"].InnerText;
			string typeSpell = persoNode["type"].InnerText;
			int instantSpell = int.Parse(persoNode["instant"].InnerText);
			int nombreTourSpell = int.Parse(persoNode["nombretour"].InnerText);
			int damageSpell = int.Parse(persoNode["damage"].InnerText);
			int constdamage = int.Parse(persoNode["damage"].InnerText);
			List<int> listDamageDot = new List<int>();
			foreach (XmlNode persoNodeDamageDot in persoNode.SelectNodes("damagedot/damage"))
       		{
				listDamageDot.Add(int.Parse(persoNodeDamageDot.InnerText));
			}
			int[] DamageDot = listDamageDot.ToArray();
			int costSpell = int.Parse(persoNode["cost"].InnerText);
			int gainSpell = int.Parse(persoNode["gain"].InnerText);
			string descriptionSpell = persoNode["description"].InnerText;
			Texture2D wImage = Resources.Load("ImagesSpells/" + idSpell, typeof(Texture2D)) as Texture2D;
            if (wImage == null)
            {
                Debug.LogWarning(idSpell + " -> texture missing");
            }
			Spells spell = new Spells(idSpell, nameSpell, typeSpell, instantSpell, nombreTourSpell, damageSpell, constdamage, DamageDot, costSpell, gainSpell, descriptionSpell, wImage, false);
			for(int i=0;i<DamageDot.Length;i++){//Obliger de faire ça sinon le tableau de dommage dot est egale a 0 de partout
				spell.setDamageDot(i,DamageDot[i]);
			}	
			spellsDictionary.Add(idSpell, spell);
		}
	}
	public Texture2D ImagePerso(int idclass){
			Texture2D wImage = Resources.Load("ImagesClasses/" + idclass, typeof(Texture2D)) as Texture2D;
            if (wImage == null)
            {
                Debug.LogWarning(idclass + " -> texture missing");
            }	
		   return wImage;
	}
	
	/// <summary>
    /// Retourne une liste de spells en passant un perso
    /// </summary>
    /// 
    
	public List<Spells> GetSpellsPersos(Personnage personnage){
		
		string persoName = personnage.Nom;
		int[] listIntSpells = classDictionary[persoName];
		List<Spells> listSpells = new List<Spells>();

		foreach (int i in listIntSpells)
		{
			listSpells.Add(spellsDictionary[i]);
			Debug.Log(spellsDictionary[i].ToString());
		}
		return listSpells;
	}
}
